#include "backupprogresswidget.h"

BackupProgressWidget::BackupProgressWidget(QWidget *parent) :
    QWidget(parent)
{
    QVBoxLayout *centralLayout = new QVBoxLayout();
    this->setLayout(centralLayout);

    QLabel *activityLabel = new QLabel(tr("Current activity"));
    centralLayout->addWidget(activityLabel);

    activityMessages = new QTextEdit();
    activityMessages->setReadOnly(true);
    activityMessages->setStyle(new QCommonStyle);
    centralLayout->addWidget(activityMessages, 1);

    QLabel *progressMessagesLabel= new QLabel(tr("Current activity output"));
    centralLayout->addWidget(progressMessagesLabel);

    taskProgressMessages = new QTextEdit();
    taskProgressMessages->setReadOnly(true);
    taskProgressMessages->setStyle(new QCommonStyle);
    centralLayout->addWidget(taskProgressMessages, 2);

    QHBoxLayout *progressBarLayout = new QHBoxLayout();
    centralLayout->addLayout(progressBarLayout);

    progressLabel = new QLabel(tr("Progress"));
    progressLabel->hide();
    progressBarLayout->addWidget(progressLabel);

    progress = new QProgressBar();
    progress->hide();
    progressBarLayout->addWidget(progress);

    QHBoxLayout *buttonLayout = new QHBoxLayout();
    centralLayout->addLayout(buttonLayout);

    pauseButton =new QPushButton(tr("Pause"));
    pauseButton->setObjectName("pauseButton");
    connect(pauseButton, &QPushButton::clicked, this, &BackupProgressWidget::onPauseButtonClicked);
    buttonLayout->addWidget(pauseButton);

    QPushButton *stopButton = new QPushButton(tr("Stop"));
    stopButton->setObjectName("stopButton");
    connect(stopButton, &QPushButton::clicked, this, &BackupProgressWidget::onStopButtonClicked);
    buttonLayout->addWidget(stopButton);
}

void BackupProgressWidget::onProgressMessageChanged(const QString &message)
{
    this->activityMessages->append(QTime::currentTime().toString() + ": " + message);
}

void BackupProgressWidget::onProgressMaximumChanged(quint64 maximum)
{
    currentMaximum = maximum;
    currentValue = 0;
    qDebug() << "CurrentMaximum set to :" << currentMaximum;
    this->progress->show();
    this->progressLabel->show();
    this->progress->setMaximum(4096);
    this->progress->setValue(0);
    this->taskProgressMessages->clear();
}

void BackupProgressWidget::onProgressValueAdd(quint64 value)
{
    currentValue += value;
    qDebug() << "CurrentValue is:" << currentValue;
    this->progress->setValue((currentValue*4096)/currentMaximum);
    if(currentValue == currentMaximum) {
        this->progress->hide();
        this->progressLabel->hide();
    }
}

void BackupProgressWidget::onNewMessage(const QString &message)
{
    this->taskProgressMessages->append(message);
}

void BackupProgressWidget::onPauseButtonClicked()
{
    if(pauseButton->text() == tr("Pause"))
        pauseButton->setText(tr("Resume"));
    else
        pauseButton->setText(tr("Pause"));
    emit pauseButtonClicked();
}

void BackupProgressWidget::onStopButtonClicked()
{
    emit stopButtonClicked();
}
